﻿
Partial Class store_transcripts
    Inherits CSearchPage

#Region "Data"
    Private m_products As CProductList
    Public ReadOnly Property [Products]() As CProductList
        Get
            If IsNothing(m_products) Then
                m_products = CProduct.Cache.Active.Sermons.HavingTranscripts

                If SpeakerId > 0 Then m_products = m_products.GetBySpeaker1or2Id(SpeakerId)
                m_products = m_products.Search(Search)

                If String.IsNullOrEmpty(Search) Then m_products = m_products.SortByCreatedDescName 'otherwise sort by relevance
            End If
            Return m_products
        End Get
    End Property
#End Region

#Region "Menu"
    Protected Overrides ReadOnly Property TopPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhTopLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SubPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhSubLinks
        End Get
    End Property
    Protected Overrides Function SideLinkUrl(t As SchemaMikeConnellMinistries.CProductList.CTagFreq) As String
        Return CSitemap.Transcripts(t.Tag, SpeakerId)
    End Function
    Protected Overrides Function SideLinkName(t As SchemaMikeConnellMinistries.CProductList.CTagFreq) As String
        Return CUtilities.NameAndCount(t.Tag, CProduct.Cache.Active.HavingTranscripts.Search(t.Tag))
    End Function
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        'Dropdowns
        Dim p As CProductList = CProduct.Cache.Active.Sermons.HavingTranscripts
        For Each i As CSpeaker In CProduct.Cache.Active.Sermons.HavingTranscripts.Speakers
            CDropdown.Add(ddSpeaker, CUtilities.NameAndCount(CUtilities.Truncate(i.SpeakerName), p.GetBySpeaker1or2Id(i.SpeakerId)), i.SpeakerId)
        Next
        CDropdown.BlankItem(ddSpeaker, "-- Any Speaker --", 0)
        CDropdown.SetValue(ddSpeaker, SpeakerId)
        If SpeakerId > 0 AndAlso ddSpeaker.SelectedIndex <= 0 Then
            CDropdown.Add(ddSpeaker, CSpeaker.Cache.GetById(SpeakerId).SpeakerName, SpeakerId)
            CDropdown.SetValue(ddSpeaker, SpeakerId)
        End If

        lnkKindle.NavigateUrl = CSitemap.External_Amazon
        lnkSmash.NavigateUrl = CSitemap.External_Smashwords
        lnkDonate.NavigateUrl = CSitemap.External_Donations


        'Search state (from querystring)
        txtSearch.Text = Me.Search

        'Tweak MetaDesc
        If SpeakerId > 0 AndAlso SpeakerId <> ESpeaker.MikeConnell Then
            MetaDescription = String.Concat(CSpeaker.Cache.GetById(SpeakerId).SpeakerName, " ", MetaDescription)
        End If
    End Sub
    Protected Overrides Sub PagePreRender()
        'Paging and display
        Dim list As CProductList = Products
        For Each i As CProduct In ctrlPaging.display(list)
            UCTranscript(plh).Display(i, list)
        Next

        'Side Links
        Dim limit As Integer = 158
        If Products.Count < 50 Then limit = 3.1 * Products.Count
        For Each i As CProductList.CTagFreq In CProduct.Cache.Active.Tags
            If limit = 0 Then Exit For
            limit -= 1
            If limit Mod 2 = 0 Then
                SideLink(colLeft, i)
            Else
                SideLink(colRight, i)
            End If
        Next

        'Emphasise active filters
        If CDropdown.GetByte(ddSpeaker) > 0 Then ddSpeaker.ForeColor = Drawing.Color.DarkBlue
        If txtSearch.Text.Length > 0 Then txtSearch.ForeColor = Drawing.Color.DarkBlue
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click, ddSpeaker.SelectedIndexChanged
        Response.Redirect(CSitemap.Transcripts(txtSearch.Text, CDropdown.GetInt(ddSpeaker)))
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCTranscript(ByVal target As Control) As store_UCTranscript
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.Store_UCTranscript)
        target.Controls.Add(ctrl)
        Return CType(ctrl, store_UCTranscript)
    End Function
    Private Shared Function UCAdsenseSide(ByVal target As Control) As usercontrols_UCAdsenseSide
        Dim ctrl As Control = target.Page.LoadControl("~/usercontrols/UCAdsenseSide.ascx")
        target.Controls.Add(ctrl)
        Return CType(ctrl, usercontrols_UCAdsenseSide)
    End Function
#End Region

End Class

